* ==============================================================================
*
* Date:	Summer 2024
* Paper: Climate shocks, regional favoritism and trust in leaders: Insights from droughts in Africa
*
* This creates Table S10
*
* ==============================================================================

* Open dataset
cd $PATH
use tr_rep, clear

* Global macros

* Control sets
global xcont "age age2 male i.ed_ord"

global fecont "urban ea_svc_a ea_svc_b ea_svc_c ea_fac_a ea_fac_b ea_fac_c ea_fac_d ea_fac_e ea_sec_a ea_sec_b grs0 grsem0 grsl cropland_d ln_alt ln_dist_cit ln_alt_sd ln_dist_bor ln_dist_coa ln_dist_riv ln_dist_roa i.year i.month i.uid1 countrytrend*"

* Clustering level
global clustervar "cidgrid01"

**#

* Clean sample
keep if precision_code<=3
global zxcont "age age2 male ed_ord"
global zfecont "urban ea_svc_a ea_svc_b ea_svc_c ea_fac_a ea_fac_b ea_fac_c ea_fac_d ea_fac_e ea_sec_a ea_sec_b grs0 grsem0 grsl cropland_d ln_alt ln_dist_cit ln_alt_sd ln_dist_bor ln_dist_coa ln_dist_riv ln_dist_roa year"

foreach var of varlist $zxcont $zfecont {
	drop if `var'==.
}

* Standardize
egen z=std(aiclwmcgrwsp)
replace aiclwmcgrwsp=z
drop z


* Control set clean up before acreg
reg tr_pres_ord aiclwmcgrwsp $xcont $fecont, vce(cluster $clustervar)
keep if e(sample)
drop countrytrend*

capture drop z
gen z=year-2002
sort id0
egen zid0=group(id0)

su zid0, det
local zmax = r(max)
forvalues i=1(1)`zmax' {
qui gen countrytrend`i'=z if zid0==`i'
}

forvalues i=1(1)`zmax' {
qui replace countrytrend`i'=0 if countrytrend`i'==.
}
drop z zid0

reg tr_pres_ord aiclwmcgrwsp $xcont $fecont, vce(cluster $clustervar)

* Acreg globals
global xcont_acreg "age age2 male"
global fecont_acreg "urban ea_svc_a ea_svc_b ea_svc_c ea_fac_a ea_fac_b ea_fac_c ea_fac_d ea_fac_e ea_sec_a ea_sec_b grs0 grsem0 grsl cropland_d ln_alt ln_dist_cit ln_alt_sd ln_dist_bor ln_dist_coa ln_dist_riv ln_dist_roa i.year i.month countrytrend1-countrytrend9 countrytrend11-countrytrend33"

capture drop yrmon
gen yrmon=0
replace yrmon=(year-2002)*12+month
su yrmon

* Table name
global tabname "spatcorrse2"

* Regression(s)
timer clear
local x=0
local z = ""

*c1
local x=`x'+1
di "Column `x'"
timer on `x'
xi: acreg tr_pres_ord aiclwmcgrwsp $xcont_acreg $fecont_acreg , id(abcluster) time(yrmon)  ///
latitude(latitude) longitude(longitude) spatial pfe1(ed_ord) pfe2(uid1) correctr2 ///
lag(201) dist(500) dropsingletons hac
local z`x' m`x'
local z = "`z'" + " `z`x''"
est store m`x'
local m="c`x'"
estimates save $tabname`m', replace
timer off `x'
timer list `x'


* Post-table 
timer list   
di "`z'"

* Simplified table generation
global keepvars "aiclwmcgrwsp"

* In Stata output window
esttab `z' , ///
	addnotes("Dependent variable: Trust in President" "Estimated with OLS." "In parenthesis are spatial HAC standard errors allowing for a spatial correlation within a 500 km radius of an Afrobarometer enumeration area cluster location and infinite serial correlation.") ///
	keep($keepvars) ///
	order($keepvars) ///
	cells(b(star fmt(%8.3f) ) se( par fmt(%6.3f)) ) ///
	stats(N r2, fmt(%9.0f %9.2f)) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) r2(3) ///
	legend


* Excel-file
estout `z' using $OUTPATH/$tabname.xls, replace ///
	postfoot("Dependent variable: Trust in President" "Estimated with OLS." "In parenthesis are spatial HAC standard errors allowing for a spatial correlation within a 500 km radius of an Afrobarometer enumeration area cluster location and infinite serial correlation.") ///
	keep($keepvars) ///
	order($keepvars) ///
	cells(b(star fmt(%8.3f) ) se( par(`"="("' `")""') fmt(%6.3f)) ) ///
	stats(N r2, fmt(%9.0f %9.2f)) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) ///
	legend

